package com

import java.io.*

/***
 * 合并文件
 * @author boge
 */
class MergeFile {

    init {
        start()
    }

    private fun start() {
        val file = File("/Users/boge/Downloads/base/assets/bin/Data/")

        if (!file.exists()) {
            System.err.println("不存在路径")
            return
        }

        val ma = HashMap<String, MutableList<String>>()
        getAllFile(file, ma)

        val keys = ma.keys.toTypedArray<String>()

        val list = ma[keys[0]]!!
        val files = list.toTypedArray<String>()
        mergeFiles(files, "/Users/boge/Downloads/base/0.assets")
    }

    /**
     * 获取所有的文件
     * @param file
     * @return
     */
    private fun getAllFile(file: File, ma: MutableMap<String, MutableList<String>>) {
        val files = file.listFiles()
        var fileName: String
        var fileName1: String
        var strList: MutableList<String>
        for (i in files.indices) {
            if (files[i].isDirectory) {
                getAllFile(files[i], ma)
            } else {
                fileName = files[i].name
                if (fileName.indexOf(".assets.split") != -1) {
                    fileName1 = getName(fileName)
                    if (ma.containsKey(fileName1)) {
                        strList = ma[fileName1]!!
                    } else {
                        strList = mutableListOf()
                        ma[fileName1] = strList
                    }
                    strList.add(files[i].path)
                    //					System.out.println(fileName+" | "+fileName1);
                }
            }
        }
    }

    fun getName(value: String): String {
        var str = value
        val index = value.indexOf(".")
        if (index != -1) {
            str = value.substring(0, index)
        }
        return str
    }

    fun mergeFiles(fpaths: Array<String>?, resultPath: String?): Boolean {
        if (fpaths.isNullOrEmpty() || resultPath == null) {
            return false
        }
        if (fpaths.size == 1) {
            return File(fpaths[0]).renameTo(File(resultPath))
        }

        val files = arrayOfNulls<File>(fpaths.size)
        for (i in fpaths.indices) {
            files[i] = File(fpaths[i])
            if (!files[i]!!.exists() || !files[i]!!.isFile) {
                return false
            }
        }

        val resultFile = File(resultPath)

        try {
            val resultFileChannel = FileOutputStream(resultFile, true).channel
            for (i in fpaths.indices) {
                val blk = FileInputStream(files[i]).channel
                resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size())
                blk.close()
            }
            resultFileChannel.close()
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
            return false
        } catch (e: IOException) {
            e.printStackTrace()
            return false
        }

        //	    for (int i = 0; i < fpaths.length; i ++) {
//	        files[i].delete();
//	    }
        return true
    }
}
